﻿#include "detecttest.h"

using namespace std;
using namespace cv;

void detecttest::Init(cv::Mat picture)
{

	namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"
	//imshow("Control".CV_WINDOW_AUTOSIZE);
	waitKey(30);


	//Create trackbars in "Control" window
	createTrackbar("LowH", "Control", &iLowH, 255); //Hue (0 - 179)
	createTrackbar("HighH", "Control", &iHighH, 255);


	createTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
	createTrackbar("HighS", "Control", &iHighS, 255);

	createTrackbar("LowV", "Control", &iLowV, 255);//Value (0 - 255)
	createTrackbar("HighV", "Control", &iHighV, 255);

	Mat PI = picture;//imread("1_dd.jpg");

	imgLines = Mat::zeros(PI.size(), CV_8UC3);;
}

void detecttest::detect(cv::Mat picture, vector<vector <double> > &pixels)
{
	vector<double> coordinate(2,0.0);

	if (exetime == 0)
	{
		Init(picture);
		exetime = 1;
	}


	Mat imgHSV;

	cvtColor(picture, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

	Mat imgThresholded;

	inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

   //morphological opening (removes small objects from the foreground)
	erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
	dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

	//morphological closing (removes small holes from the foreground)
	dilate(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
	erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));

	//Calculate the moments of the thresholded image
	Moments oMoments = moments(imgThresholded);

	double dM01 = oMoments.m01;
	double dM10 = oMoments.m10;
	double dArea = oMoments.m00;

	// if the area <= 10000, I consider that the there are no object in the image and it's because of the noise, the area is not zero 
	if (dArea > 10000)
	{
		//calculate the position of the ball
		double posX = dM10 / dArea;
		double posY = dM01 / dArea;

	//	cout << "posX  " << posX;
	//	cout << "   posY  " << posY << endl;

		coordinate[0] = posX;
		coordinate[1] = posY;

		if (iLastX >= 0 && iLastY >= 0 && posX >= 0 && posY >= 0)
		{
			//Draw a red line from the previous point to the current point
			line(imgLines, Point(posX, posY), Point(iLastX, iLastY), Scalar(0, 0, 255), 2);
		}

		iLastX = posX;
		iLastY = posY;

		pixels.push_back(coordinate);
	}
	//	cout << "iLastX  " << iLastX ;
	//	cout << "   iLastY  " << iLastX << endl;

	namedWindow("Thresholded Image");
	imshow("Thresholded Image", imgThresholded); //show the thresholded image
	waitKey(30);

	picture = picture + imgLines;
	namedWindow("Original");
	imshow("Original", picture); //show the original image
	waitKey(30);


	//return coordinate;
}


